package com.ms.es.result.impl;

import com.ms.common.exception.ESQueryException;
import com.ms.common.utils.GsonUtil;
import com.ms.es.sql.ColumnInfo;
import com.ms.es.sql.EsSqlResult;
import com.ms.es.sql.SqlDesc;
import org.apache.ibatis.exceptions.TooManyResultsException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EsSqlResultHandler extends BaseResultHandler {

    @Override
    public Object handler(String result, SqlDesc sqlDesc) {
        try {
            EsSqlResult queryResult = GsonUtil.str2Object(result, EsSqlResult.class);
            if (queryResult.getStatus() > 0) {
                throw new ESQueryException(result);
            }
            List<Object> resultList = new ArrayList<>();
            List<Object[]> data = queryResult.getRows();
            List<ColumnInfo> columList = queryResult.getColumns();
            if (data.isEmpty()) {
                return resultList;
            }
            int columSize = columList.size();
            for (Object[] obj : data) {
                Map<String, Object> map = new HashMap<>();
                for (int i = 0; i < columSize; i++) {
                    ColumnInfo info = columList.get(i);
                    map.put(info.getName(), obj[i]);
                }
                Object objResult = GsonUtil.str2Object(GsonUtil.obj2String(map), sqlDesc.getResultType());
                resultList.add(objResult);
            }
            if (sqlDesc.isReturnsMany() || sqlDesc.isReturnsVoid()) {
                return resultList;
            } else {
                if (resultList.size() == 1) {
                    return resultList.get(0);
                } else {
                    throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + resultList.size());
                }
            }
        } catch (Exception e) {
            throw new ESQueryException(result, e);
        }
    }

}
